package com.qq.mobilemanager.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qq.mobilesafe.R;
import com.qq.mobilesafe.service.AddressService;
import com.qq.mobilesafe.service.WatchDogService;
import com.qq.mobilesafe.utils.ServiceStatusUtils;
import com.qq.mobilesafe.utils.SharedPreferencesUtils;
import com.qq.mobilesafe.utils.SystemInfoUtils;
import com.qq.mobilesafe.view.SettingClickView;
import com.qq.mobilesafe.view.SettingItemView;

public class SettingActivity extends Activity {

	@ViewInject(R.id.siv_update)
	private SettingItemView sivUpdate;

	@ViewInject(R.id.siv_address)
	private SettingItemView sivAddress;

	@ViewInject(R.id.scv_address_style)
	private SettingClickView scv_address_style;

	@ViewInject(R.id.scv_address_location)
	private SettingClickView scv_address_location;

	@ViewInject(R.id.siv_watch_dog)
	private SettingItemView siv_watch_dog;

	// private SharedPreferences preferences;

	private Intent watchDogIntent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		ViewUtils.inject(this);

		// preferences = getSharedPreferences("config", MODE_PRIVATE);
		initUpdateView();
		initAddressView();
		initAddressStyle();
		initAddressLocation();

		watchDogIntent = new Intent(this, WatchDogService.class);
		siv_watch_dog.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (siv_watch_dog.isChecked()) {
					siv_watch_dog.setChecked(false);
					// 停止拦截服务
					stopService(watchDogIntent);
				} else {
					siv_watch_dog.setChecked(true);
					// 开启拦截服务
					startService(watchDogIntent);
				}
			}
		});

	}

	@Override
	protected void onStart() {

		// 判断服务的运行状态
		boolean running = SystemInfoUtils.isServiceRunning(this,
				"com.qq.mobilesafe.service.WatchDogService");
		if (running) {
			siv_watch_dog.setChecked(true);
		} else {
			siv_watch_dog.setChecked(false);
		}

		super.onStart();

	}

	private void initUpdateView() {
		boolean autoUpdate = SharedPreferencesUtils.getBoolean(this,
				"autoUpdate", true);
		if (autoUpdate) {
			sivUpdate.setChecked(true);
		} else {
			sivUpdate.setChecked(false);
		}

		sivUpdate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (sivUpdate.isChecked()) {
					sivUpdate.setChecked(false);
					// SharedPreferencesUtils.saveBoolean(SettingActivity.this,
					// "autoUpdate", true);
				} else {
					sivUpdate.setChecked(true);
					// SharedPreferencesUtils.saveBoolean(SettingActivity.this,
					// "autoUpdate", false);
				}
				SharedPreferencesUtils.saveBoolean(SettingActivity.this,
						"autoUpdate", sivUpdate.isChecked());
			}
		});
	}

	public void initAddressView() {
		boolean runningservice = ServiceStatusUtils.isServiceRunning(this,
				AddressService.class);
		if (runningservice) {
			sivAddress.setChecked(true);

		} else {
			sivAddress.setChecked(false);
		}

		sivAddress.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (sivAddress.isChecked()) {
					sivAddress.setChecked(false);
					stopService(new Intent(SettingActivity.this,
							AddressService.class));

				} else {
					sivAddress.setChecked(true);
					startService(new Intent(SettingActivity.this,
							AddressService.class));
				}
			}
		});
	}

	final String[] items = new String[] { "半透明", "活力橙", "卫士蓝", "金属灰", "苹果绿" };

	private void initAddressStyle() {
		scv_address_style.setTile("归属地提示框风格");

		int style = SharedPreferencesUtils.getInt(SettingActivity.this,
				"address_style", 0);

		scv_address_style.setDesc(items[style]);
		scv_address_style.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showSingleChooseDailog();

			}
		});

	}

	protected void showSingleChooseDailog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("归属地提示框风格");

		int style = SharedPreferencesUtils.getInt(SettingActivity.this,
				"address_style", 0);

		builder.setSingleChoiceItems(items, style,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						SharedPreferencesUtils.putInt(SettingActivity.this,
								"address_style", which);
						dialog.dismiss();
						scv_address_style.setDesc(items[which]);
					}
				});
		builder.setNegativeButton("取消", null);
		builder.show();
	}

	public void initAddressLocation() {
		scv_address_location.setTile("归属地提示框显示位置");
		scv_address_location.setDesc("设置归属地提示框的显示位置");
		scv_address_location.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(SettingActivity.this,
						DragViewActivity.class));
			}
		});
	}
}
